//
//  GalleryHeadMoreViewController.m
//  ArtDog
//
//  Created by dllo on 16/9/10.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "GalleryHeadMoreViewController.h"
#import "BaseCollectionView.h"
#import "HLMacro.h"
#import "UIColor+Common.h"
#import "ZWYNetTool.h"
#import "GalleryResultModel.h"
#import "GalleryHotColumnList.h"
#import "MJRefresh.h"
#import "MJRefreshComponent+RefreshStyle.h"
#import "NSString+Common.h"
#import "GalleryHeadMoreCollectionCell.h"

@interface GalleryHeadMoreViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) BaseCollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *loadArray;

@property (nonatomic, assign) NSInteger pageIndex;

@end

@implementation GalleryHeadMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self setupNavigationBar];
    [self createCollectionView];
    [self refreshCollectionView];
}

- (void)setupNavigationBar
{
    self.navigationItem.title = @"专题";
}

- (void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    _collectionView = [[BaseCollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:flowLayout];
    [self.view addSubview:_collectionView];
    flowLayout.minimumLineSpacing = 5;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH / 3 + 60);
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerNib:[UINib nibWithNibName:@"GalleryHeadMoreCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"galleryHeadMoreCollectionCell"];
}

#pragma mark - 数据请求
- (void)handleData
{
    _pageIndex = 1;
    NSString *urlString = [self getUrlString];
    [ZWYNetTool GET:urlString andBody:nil andHeader:HOSTHEADER andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _loadArray = [NSMutableArray array];
        GalleryResultModel *model = [GalleryResultModel modelWithDic:result[@"data"]];
        for (GalleryHotColumnList *hotColumn in model.hotColumnList) {
            [_loadArray addObject:hotColumn];
        }
        [_collectionView.mj_header endRefreshing];
        [_collectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        [_collectionView.mj_header endRefreshing];
        NSLog(@"%@", error);
    }];
}

- (void)handleLoadData
{
    NSString *urlString = [self getUrlString];
    [ZWYNetTool GET:urlString andBody:nil andHeader:HOSTHEADER andResponse:ZWYJSON andSuccessBlock:^(id result) {
        GalleryResultModel *model = [GalleryResultModel modelWithDic:result[@"data"]];
        for (GalleryHotColumnList *hotColumn in model.hotColumnList) {
            [_loadArray addObject:hotColumn];
        }
        [_collectionView.mj_footer endRefreshing];
        [_collectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        [_collectionView.mj_footer endRefreshing];
        NSLog(@"%@", error);
    }];
}

- (NSString *)getUrlString
{
    NSString *urlString;
    
    urlString = [NSString getURLStringWith:HOST andPaths:@[@"artgoer/api/v1/user", [NSString getUserDefaultId], @"v4/column/columnList"] andParams:@{@"pageIndex": [NSString stringWithFormat:@"%ld", _pageIndex], @"token": [NSString getUserDefaultToken]}];
    
    return urlString;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _loadArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GalleryHeadMoreCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"galleryHeadMoreCollectionCell" forIndexPath:indexPath];
    cell.hotColumnModel = _loadArray[indexPath.item];
    
    return cell;
    
}

#pragma mark - 刷新
- (void)refreshCollectionView
{
    _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self handleData];
    }];
    [_collectionView.mj_header mjRefreshStateHeaderStyle];
    
    _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (_loadArray.count % 10 == 0) {
            _pageIndex = _loadArray.count / 10 + 1;
        } else {
            _pageIndex = _loadArray.count / 10 + 2;
        }
        [self handleLoadData];
    }];
    [_collectionView.mj_footer mjRefreshAutoStateFooterStyle];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
